package com.ltv.xiangqiplugin.views;

import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.ltv.xiangqiplugin.models.BoardSizeEnum;
import com.ltv.xiangqiplugin.models.Messages;
import com.ltv.xiangqiplugin.models.MessagesObserver;

public class GameActionView extends JPanel implements MessagesObserver {
	private JButton reverse;
	private JButton first;
	private JButton prev;
	private JButton next;
	private JButton last;
	private JButton rollback;
	private JButton html;
	private JButton option;
	
	private ChessApp mainApp;
	
	JPanel mainPanel;
	
	public GameActionView(ChessApp mainApp) {
		this.mainApp = mainApp;
	    mainPanel = new JPanel();
	    
	    reverse = new JButton("");
	    ImageIcon reverseIcon = new ImageIcon(getClass().getResource("/buttons/reverse.png"));
	    reverse.setIcon(reverseIcon);
	    reverse.setActionCommand("REVERSE");
	    reverse.addActionListener(mainApp.getGameActionControl());
	    mainPanel.add(reverse);
	    
	    first = new JButton("");
	    ImageIcon firstIcon = new ImageIcon(getClass().getResource("/buttons/first.png"));
	    first.setIcon(firstIcon);
	    first.setActionCommand("FIRST");
	    first.addActionListener(mainApp.getGameActionControl());
	    mainPanel.add(first);
	    
	    prev = new JButton("");
	    ImageIcon prevIcon = new ImageIcon(getClass().getResource("/buttons/prev.png"));
	    prev.setIcon(prevIcon);
	    prev.setActionCommand("PREV");
	    prev.addActionListener(mainApp.getGameActionControl());
	    mainPanel.add(prev);
	    
	    next = new JButton("");
	    ImageIcon nextIcon = new ImageIcon(getClass().getResource("/buttons/next.png"));
	    next.setIcon(nextIcon);
	    next.setActionCommand("NEXT");
	    next.addActionListener(mainApp.getGameActionControl());
	    mainPanel.add(next);
	    
	    last = new JButton("");
	    ImageIcon lastIcon = new ImageIcon(getClass().getResource("/buttons/last.png"));
	    last.setIcon(lastIcon);
	    last.setActionCommand("LAST");
	    last.addActionListener(mainApp.getGameActionControl());
	    mainPanel.add(last);
	    
	    ImageIcon rollbackIcon = new ImageIcon(getClass().getResource("/buttons/refresh.png"));
	    rollback = new JButton("");
	    rollback.setIcon(rollbackIcon);
	    rollback.setEnabled(false);
	    rollback.setActionCommand("ROLLBACK");
	    rollback.addActionListener(mainApp.getGameActionControl());
	    if (!mainApp.isStandaloneApp()) {
	    	mainPanel.add(rollback);
	    }
	    
	    ImageIcon htmlIcon = new ImageIcon(getClass().getResource("/buttons/html.png"));
	    html = new JButton("");
	    html.setIcon(htmlIcon);
	    html.setActionCommand("HTML");
	    html.addActionListener(mainApp.getGameActionControl());
	    mainPanel.add(html);
	    
	    ImageIcon optionIcon = new ImageIcon(getClass().getResource("/buttons/option.png"));
	    option = new JButton("");
	    option.setIcon(optionIcon);
	    option.setActionCommand("OPTION");
	    option.addActionListener(mainApp.getGameActionControl());
	    // panel.add(option); // This button will be used in windows application
	    
	    this.add(mainPanel);
	    
	}

	public void enableRollbackButton() {
		rollback.setEnabled(true);
	}
	
	public void disableRollbackButton() {
		rollback.setEnabled(false);
	}
	
	public boolean isEnableRollback() {
		return rollback.isEnabled();
	}

	private void resizeButtons(BoardSizeEnum size) {
		int w, h;
		if (size == BoardSizeEnum.SMALL) {
			w = 32;
			h = 16;
		} else if (size == BoardSizeEnum.MEDIUM) {
			w = 40;
			h = 24;
		} else if (size == BoardSizeEnum.LARGE) {
			w = 48;
			h = 32;
		} else {
			w = 40;
			h = 24;
		}
		reverse.setPreferredSize(new Dimension(w, h));
		first.setPreferredSize(new Dimension(w, h));
		prev.setPreferredSize(new Dimension(w, h));
		next.setPreferredSize(new Dimension(w, h));
		last.setPreferredSize(new Dimension(w, h));
		rollback.setPreferredSize(new Dimension(w, h));
		html.setPreferredSize(new Dimension(w, h));
		option.setPreferredSize(new Dimension(w, h));
	}
	
	public void changeToInactiveMode() {
		this.removeAll();
		JPanel panel = new JPanel();
		panel.add(reverse);
		panel.add(first);
		panel.add(prev);
		panel.add(next);
		panel.add(last);
		this.add(panel);
	}

	@Override
	public void messagesChanged() {
		reverse.setToolTipText(Messages.getInstance().getProperty("button.reverse.tooltip"));
		first.setToolTipText(Messages.getInstance().getProperty("button.first.tooltip"));
		prev.setToolTipText(Messages.getInstance().getProperty("button.previous.tooltip"));
		next.setToolTipText(Messages.getInstance().getProperty("button.next.tooltip"));
		last.setToolTipText(Messages.getInstance().getProperty("button.last.tooltip"));
		rollback.setToolTipText(Messages.getInstance().getProperty("button.refresh.tooltip"));
		html.setToolTipText(Messages.getInstance().getProperty("button.html.tooltip"));
		option.setToolTipText(Messages.getInstance().getProperty("button.option.tooltip"));
		
		resizeButtons(Messages.getInstance().getBoardSize());
		mainPanel.revalidate();
	}
}
